package com.wuzhen.wzzt.controller;

import com.wuzhen.wzzt.core.ret.RetCode;
import com.wuzhen.wzzt.core.ret.RetResponse;
import com.wuzhen.wzzt.core.ret.RetResult;
import com.wuzhen.wzzt.model.EnrollUser;
import com.wuzhen.wzzt.service.EnrollService;
import com.wuzhen.wzzt.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 *
 * 报名接口
* @Description: EnrollController
* @author zhengzheng
* @date 2022年4月2日 11:28
*/
@RestController
@RequestMapping("/enroll")
@Api(tags = {"用户报名接口"}, description = "EnrollController", position=1)
public class EnrollController {

    private static Logger logger = LoggerFactory.getLogger(EnrollController.class);

    @Resource
    private EnrollService enrollService;

    @Resource
    private RedisService redisService;

    @PostMapping("/add")
    @ApiOperation(value = "用户报名接口", notes = "用户报名接口")
    public RetResult<Integer> insert(@RequestHeader(value = "thirdSessionKey",required = true) String thirdSessionKey,@RequestBody  EnrollUser enrollUser) throws Exception{
        logger.info("用户报名接口，入参:enrollUser:{},thirdSessionKey:{}",enrollUser.toString(),thirdSessionKey);
        String openIdStr = redisService.get(thirdSessionKey);
        logger.info("根据token：{}，查询到oepnid结果:{}",thirdSessionKey,openIdStr);
        if(openIdStr==null){
            return RetResponse.makeRsp(RetCode.WX_SESSION_TIMEOUT.code,"会话失效");
        }else{
            openIdStr= openIdStr.split("#")[1];
            enrollUser.setOpenId(openIdStr);
        }
        enrollUser.setCreateTime(new Date());
        enrollUser.setUpdateTime(new Date());
        Map<Long,Map<String,Long>> map =  enrollService.selectSeq();
        Long maxId = map.entrySet().iterator().next().getKey();
        logger.info("获取数据库最大ID："+maxId);
        String hexId = hexStrFormat_12(maxId);
        logger.info("生成二维码内容值,qr_content：{}",hexId);
        enrollUser.setQrCode(hexId);
    	Integer state = enrollService.insert(enrollUser);
        logger.info("用户报名接口入库结果:{}",state);
        if (state==1){
            return RetResponse.makeOKRsp(state);
        }else {
            return RetResponse.makeErrRsp("报名失败");
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "报名信息修改接口", notes = "报名信息修改接口")
    public RetResult<Integer> update(@RequestHeader(value = "thirdSessionKey",required = true) String thirdSessionKey,@RequestBody  EnrollUser enrollUser) throws Exception{
        logger.info("用户报名接口，入参:enrollUser:{},thirdSessionKey:{}",enrollUser.toString(),thirdSessionKey);
        String openIdStr = redisService.get(thirdSessionKey);
        logger.info("根据token：{}，查询到oepnid结果:{}",thirdSessionKey,openIdStr);
        if(openIdStr==null){
            return RetResponse.makeRsp(RetCode.WX_SESSION_TIMEOUT.code,"会话失效");
        }else{
            openIdStr= openIdStr.split("#")[1];
            enrollUser.setOpenId(openIdStr);
        }
        Integer state = enrollService.updateEnrollData(enrollUser);
        logger.info("用户报名信息修改接口入库结果:{}",state);
        if (state==1){
            return RetResponse.makeOKRsp();
        }else {
            return RetResponse.makeErrRsp("用户报名信息修改失败");
        }
    }


    /**
     * 生成12位16进制EPC值
     * @param maxid
     * @return
     */
    public String hexStrFormat_12(Long maxid) {
        String hexId = Long.toHexString(maxid+1);
        while (hexId.length()<12){
            hexId ='0'+hexId;
        }
        return  hexId;
    }

}